// Copyright (C) 2002-2008 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#ifndef __C_FPS_PARTICLE_BOX_EMITTER_H_INCLUDED__
#define __C_FPS_PARTICLE_BOX_EMITTER_H_INCLUDED__

#include "IFpsParticleEmitter.h"
#include "irrArray.h"




//! A default box emitter
class CFpsParticleEmitter : public IFpsParticleEmitter
{
public:

	//! constructor
	CFpsParticleEmitter(
		const rectf& box = rectf(),
		const vector2df& direction = vector2df(0.0f,0.03f),		
		u32 speedVariation = 0,
		u32 minParticlesPerSecond = 20,
		u32 maxParticlesPerSecond = 40,
		f32 particleWidth = 5.f, f32 particleHeight = 5.f, s32 sizeVariation = 0,
		SColor minStartColor = SColor(255,0,0,0),
		SColor maxStartColor = SColor(255,255,255,255),
		float lifeTimeMin=2.0f,
		float lifeTimeMax=4.0f,
		s32 maxAngleDegrees=0,
		s32 initialRotMin = 0, s32 initialRotMax = 0);

	//! Prepares an array with new particles to emitt into the system
	//! and returns how much new particles there are.
	virtual s32 emitt(float now, float timeSinceLastCall, SFpsParticle*& outArray);

	virtual s32 emitt2(float now, int nbParticles, SFpsParticle*& outArray);

	//! Set direction the emitter emits particles.
	virtual void setDirection( const vector2df& newDirection ) { Direction = newDirection; }

	//! Set speed variation.
	virtual void setSpeedVariation( u32 speedVariation ) { SpeedVariation = speedVariation; }

	//! Set minimum number of particles emitted per second.
	virtual void setMinParticlesPerSecond( u32 minPPS ) { MinParticlesPerSecond = minPPS; }

	//! Set maximum number of particles emitted per second.
	virtual void setMaxParticlesPerSecond( u32 maxPPS ) { MaxParticlesPerSecond = maxPPS; }

	//! Set minimum start color.
	virtual void setMinStartColor( const SColor& color ) { MinStartColor = color; }

	//! Set maximum start color.
	virtual void setMaxStartColor( const SColor& color ) { MaxStartColor = color; }

	static void reallocate( int size );
	static void free();

	//! Set box from which the particles are emitted.
	virtual void setBox( const position2df& center, float radius ) { m_box = rectf(center,dimension2df(radius,radius)); }

	virtual const rectf& getBox() const { return m_box; }
	//! Gets direction the emitter emits particles.
	virtual const vector2df& getDirection() const { return Direction; }

	//! Gets speed variation.
	virtual u32 getSpeedVariation() const { return SpeedVariation; }

	//! Gets minimum number of particles emitted per second.
	virtual u32 getMinParticlesPerSecond() const { return MinParticlesPerSecond; }

	//! Gets maximum number of particles emitted per second.
	virtual u32 getMaxParticlesPerSecond() const { return MaxParticlesPerSecond; }

	//! Gets particles width
	virtual f32 getParticleWidth() const { return ParticleWidth; }

	//! Gets particles width
	virtual f32 getParticleHeight() const { return ParticleHeight; }

	//! Gets particles size variation
	virtual s32 getSizeVariation() const { return SizeVariation; }

	//! Gets minimum start color.
	virtual const SColor& getMinStartColor() const { return MinStartColor; }

	//! Gets maximum start color.
	virtual const SColor& getMaxStartColor() const { return MaxStartColor; }

	//! Writes attributes of the object.
	virtual void serializeAttributes(IAttributes* out) const;

	//! Reads attributes of the object.
	virtual s32 deserializeAttributes(s32 startIndex, IAttributes* in);

	//! Gets min life time
	virtual float getMinLifeTime() const { return MinLifeTime; }

	//! Gets max life time
	virtual float getMaxLifeTime() const { return MaxLifeTime; }

	//| Gets max angle in degrees for XY plane
	virtual s32 getMaxAngleDegrees() const { return MaxAngleDegrees; }

	//| Gets initial rotation min angle
	virtual s32 getInitialRotMin() const { return InitialRotMin; }

	//| Gets initial rotation max angle
	virtual s32 getInitialRotMax() const { return InitialRotMax; }

	virtual void Reset()	{Time = 0;}

private:

	static array<SFpsParticle> Particles;
	rectf m_box;
	vector2df Direction;
	u32 SpeedVariation;
	u32 MinParticlesPerSecond, MaxParticlesPerSecond;
	f32 ParticleWidth, ParticleHeight;
	s32 SizeVariation;
	SColor MinStartColor, MaxStartColor;
	float MinLifeTime, MaxLifeTime;

	float Time;
	u32 Emitted;
	s32 MaxAngleDegrees;
	s32 InitialRotMin;
	s32 InitialRotMax;
};

#endif

